package com.lxyk.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 如果是登陆页面则放行
        System.out.println("uri: " + request.getRequestURI());
        String uri = request.getRequestURI();

        //接口和页面都要放行
        if(uri.contains("/login") || uri.contains("/login.jsp") || uri.contains("/checkCode") ||
                uri.contains("/register.jsp") ||uri.contains("/register") || uri.contains("/setStatus") || uri.contains("/sendMessage") ||
                uri.contains("/css") || uri.contains("/js") || uri.contains("/fonts")){
            return true;//放行
        }


        HttpSession session = request.getSession();

        // 如果用户已登陆也放行
        if(session.getAttribute("admin") != null) {
            return true;
        }

        // 用户没有登陆跳转到登陆页面
        request.getRequestDispatcher("/login.jsp").forward(request, response);
        return false;
    }
}
